using System;
using System.Collections.Generic;
using System.IO;
using System.Collections;
using System.Linq;

namespace Org.JSON
{
	public class Array : Value, IEnumerable<Value>
	{
		private List<Value> value;
		public Array()
		{
			this.value = new List<Value>();
		}

		public Array Add(Value item)
		{
			this.SafeAddCheck(item);

			this.value.Add(item);

			return this;
		}

		public Value this[ulong index]
		{
			get
			{
				return this.value[(int) index];
			}
		}

		public ulong Length { get { return (ulong)this.value.Count; }}

		public IEnumerator<Value> GetEnumerator()
	    {
	        return this.value.GetEnumerator();
	    }

		IEnumerator IEnumerable.GetEnumerator()
	    {
	        return this.GetEnumerator();
	    }

		internal override bool DoesRecursion (Value value)
		{
			return base.DoesRecursion(value) || this.value.Any(x => x.DoesRecursion(value));
		}


		public override void Serialize(TextWriter stream)
		{
			stream.Write('[');

			var first = true;

			foreach(var item in this.value)
			{

				if(first) 
				{
					first = false;
				}
				else
				{
					stream.Write(',');
				}
				item.Serialize(stream);
			}

			stream.Write(']');
		}
	}
}

